package org.example.elm_xiangmu.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.example.elm_xiangmu.po.DeliveryAddress;
import org.example.elm_xiangmu.result.Result;
import org.example.elm_xiangmu.service.DeliveryAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/DeliveryAddress")
@Tag(name="送货地址")
public class DeliveryAddressController {
    @Autowired
    private DeliveryAddressService deliveryAddressService;

    @Operation(summary = "根据用户编号查询所属送货地址")
    @GetMapping("/listDeliveryAddressByUserId")
    public Result listDeliveryAddressByUserId(@Parameter(description = "用户编号") @RequestParam String userId){
        List<DeliveryAddress> deliveryAddressList = deliveryAddressService.listDeliveryAddressByUserId(userId);
        if(deliveryAddressList == null || deliveryAddressList.size() == 0){
            return Result.error("用户id可能错误，查询不到");
        }else {
            return Result.success(deliveryAddressList,"查询用户送货地址成功");
        }

    }


    @Operation(summary = "根据送货地址编号查询送货地址")
    @GetMapping("/getDeliveryAddressById")
    public Result getDeliveryAddressById(@Parameter(description = "送货地址编号") @RequestParam String daId){
        List<DeliveryAddress> deliveryAddressList = deliveryAddressService.getDeliveryAddressById(daId);
        if(deliveryAddressList == null || deliveryAddressList.size() == 0){
            return Result.error("用户id可能错误，查询不到");
        }else {
            return Result.success(deliveryAddressList,"查询用户送货地址成功");
        }

    }

    @Operation(summary = "向送货地址表中添加一条记录")
    @PostMapping("/saveDeliveryAddress")
    public Result saveDeliveryAddress(@Parameter(description = "联系人姓名") @RequestParam String contactName,
                                      @Parameter(description = "联系人性别") @RequestParam String contactSex,
                                      @Parameter(description = "联系人电话") @RequestParam String contactTel,
                                      @Parameter(description = "送货地址") @RequestParam String address,
                                      @Parameter(description = "所属用户编号") @RequestParam String userId) {
        DeliveryAddress deliveryAddress = new DeliveryAddress();
        deliveryAddress.setContactName(contactName);
        deliveryAddress.setContactSex(contactSex);
        deliveryAddress.setContactTel(contactTel);
        deliveryAddress.setAddress(address);
        deliveryAddress.setUserId(userId);
        deliveryAddressService.saveDeliveryAddress(deliveryAddress);
        return Result.success("添加成功");

    }

    @Operation(summary = "根据送货地址编号更新送货地址信息")
    @PutMapping("/updateDeliveryAddress")
    public Result updateDeliveryAddress(@Parameter(description = "送货地址编号") @RequestParam Integer daId,
                                      @Parameter(description = "联系人姓名") @RequestParam String contactName,
                                      @Parameter(description = "联系人性别") @RequestParam String contactSex,
                                      @Parameter(description = "联系人电话") @RequestParam String contactTel,
                                      @Parameter(description = "送货地址") @RequestParam String address,
                                      @Parameter(description = "所属用户编号") @RequestParam String userId) {
        DeliveryAddress deliveryAddress = new DeliveryAddress();
        deliveryAddress.setDaId(daId);
        deliveryAddress.setContactName(contactName);
        deliveryAddress.setContactSex(contactSex);
        deliveryAddress.setContactTel(contactTel);
        deliveryAddress.setAddress(address);
        deliveryAddress.setUserId(userId);
        deliveryAddressService.updateDeliveryAddress(deliveryAddress);
        return Result.success("更新成功");

    }

    @Operation(summary = "根据送货地址编号删除一条记录")
    @DeleteMapping("/removeDeliveryAddress")
    public Result removeDeliveryAddress(@Parameter(description = "送货地址编号") @RequestParam Integer daId) {
        deliveryAddressService.removeDeliveryAddress(daId);
        return Result.success("删除成功");

    }




}
